//
//  ViewController.m
//  SingleViewCoreData
//
//  Created by Administrator on 5/27/13.
//  Copyright (c) 2013 Administrator. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize txtFieldUserName;
@synthesize txtFieldEmail;
@synthesize txtFieldPassword;

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    //    NSLog(@"hello");
    //    BOOL success = [self addUser:@"sang" email:@"nqsang@apcs.vn"];
    //    if (success) {
    //        NSLog(@"hi success");
    //    }
    
    SecureUserInformation *user = [self loginUser:@"sang" withPassword:@"secret"];
    if (user != nil) {
        NSLog(@"Login success: %@, email: %@", user.username, user.email);
        txtFieldUserName.text = user.username;
        txtFieldEmail.text = user.email;
    }
    else
        NSLog(@"no user in system");
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)btnRegister:(id)sender {
    BOOL success = [self addUser:txtFieldUserName.text password:txtFieldPassword.text email:@"nqsang@apcs.vn"];
    
    if (success) {
        NSLog(@"created account %@", txtFieldUserName.text);
    }
}

- (IBAction)btnLogIn:(id)sender {
    [self addUser:txtFieldUserName.text password:txtFieldUserName.text email:txtFieldEmail.text];
}

- (BOOL) addUser: (NSString*) username password: (NSString*) password email: (NSString*) email
{
    NSManagedObjectContext *context = self.managedObjectContext;
    UserInformation *user = [NSEntityDescription insertNewObjectForEntityForName:@"UserInformation" inManagedObjectContext:context];
    user.username = username;
    user.email = email;
    
    NSError *error;
    if (![context save:&error])
    {
        NSLog(@"Oops. Fail to insert user");
        return false;
    }
    SecureUserInformation *secureUser = [[SecureUserInformation init]alloc];
    secureUser.username = user.username;
    secureUser.email = user.email;
    
    [secureUser setPassword:password];
    
    return true;
}
- (SecureUserInformation*) loginUser: (NSString*) username withPassword:(NSString*) password
{
    NSManagedObjectContext *context = self.managedObjectContext;
    
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"UserInformation" inManagedObjectContext:context];
    

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"username = %@", username];
    [fetchRequest setEntity:entity];
    [fetchRequest setPredicate:predicate];
    
    NSError *error;
    NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
    
    
    
    if (fetchedObjects.count > 0) {
        UserInformation* user = fetchedObjects[0];
        
        SecureUserInformation *secureUser = [[SecureUserInformation init]alloc];
        secureUser.username = user.username;
        secureUser.email = user.email;

        if ([password isEqualToString:secureUser.password]) {
            return secureUser;
        }
    }
    return nil;
}


@end
